<?php
/**
 * UploadExcelExe.class.php
 * - Upload Excel
 * @author Longlt
 */
class UploadExcelExe {

	/**
	 * __construct
	 * - Use constructor object
	 */
	public function __construct () {
		// Nothing
	}
	/**
	  * CreateByListControl
	  * - Create excel from template and List Control
	  * @param $name name data control
	  * @param $dataDynamic data dynamic for input cell excel
	  * @param $dataStatic data static for input cell excel
	  * @return void
	 */
	public function getArrayData($fileName) {
		/**
		 * Load Control
		 */
		/** Create a new Reader of the type defined in $inputFileType **/
		$objPHPExcel 	= new PHPExcel();
        $objReader 		= new PHPExcel_Reader_Excel5;

		try {
			$objPHPExcel = $objReader->load(Util::pathUpload("excel") . "/" . $fileName);
		}
		catch (Exception $e){
			throw new CHttpException(404,"Không tìm thấy file {$fileName}");
		}
		$objWorksheet = $objPHPExcel->getActiveSheet();

		$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
		$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'

		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5

		$dataFromFile 	= array();
		$listFieldName 	= array();
		/*
		 * Get array with format by row Array(0 => Array(1 => field_name1,
		 		* 										 2 => field_name2),
		 * 									  1 => Array(1 => value1,
		 * 												 2 => value2))
		 */
		//$dataFromFile = $objWorksheet->toArray();

		/*
		 * Get array with format Array(0 => Array(field_name1 => value1,
		 * 										  field_name2 => value2),
		 * 							   1 => Array(field_name1 => value1,
		 * 										  field_name2 => value2))
		 */
		// Get list field name
		for ($col = 0; $col < $highestColumnIndex; $col++) {
			$listFieldName[$col] = $objWorksheet->getCellByColumnAndRow($col, 1)->getValue();
		}

		// Get list data
		$i = 0;
		for ($row = 2; $row <= $highestRow; ++$row) {
			for ($col = 0; $col <= $highestColumnIndex; ++$col) {
				$dataFromFile[$i][$listFieldName[$col]] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
			}
			$i++;
		}

		return $dataFromFile;
	}

}
?>